<script setup lang="ts">
import { onMounted, reactive, ref, watch, type Ref, computed } from "vue";
import type { DefaultConfigType } from "./index.d";
import { cloneDeep, merge } from "lodash-es";

/* ----------------- 类型声明 ----------------- */
interface DataItem {
    name: string;
    value: number | string;
}

/* ----------------- Props ----------------- */
interface Props {
    config: Ref<Partial<DefaultConfigType>>;
    data: Ref<DataItem[]>;
}

const props = withDefaults(defineProps<Props>(), {
    /* 注意：这里要用函数返回 ref，而不是直接把 ref 当默认值 */
    config: () => ref({}),
    data: () => ref([]),
});

/* ----------------- 内部状态 ----------------- */
const mergedConfig = ref<DefaultConfigType>();
const capsuleLength = ref<number[]>([]);
const capsuleValue = ref<(number | string)[]>([]);
const labelData = ref<number[]>([]);

const defaultConfig = reactive<DefaultConfigType>({
    colors: ["#37a2da", "#32c5e9", "#67e0e3", "#9fe6b8", "#ffdb5c", "#ff9f7f", "#fb7293"],
    unit: "",
    showValue: false,
});

/* ----------------- 业务逻辑 ----------------- */
const mergeConfig = () => {
    mergedConfig.value = merge(cloneDeep(defaultConfig), props.config.value || {});
};

const calcCapsuleLengthAndLabelData = () => {
    const data = props.data.value;
    if (!data.length) return;

    const newCapsuleValue = data.map(item => item.value);
    const maxValue = Math.max(...newCapsuleValue.map(Number));

    capsuleValue.value = newCapsuleValue;
    capsuleLength.value = newCapsuleValue.map(v => (maxValue ? Number(v) / maxValue : 0));

    const oneFifth = maxValue / 5;
    labelData.value = Array.from(new Set(Array.from({ length: 6 }, (_, i) => Math.ceil(i * oneFifth))));
};

const calcData = () => {
    mergeConfig();
    calcCapsuleLengthAndLabelData();
};
const nameList = computed(() => props.data.value);
/* ----------------- 监听 ----------------- */
watch(() => props.data, calcData, { deep: true });
watch(() => props.config, calcData, { deep: true });

onMounted(calcData);
</script>

<template>
    <div class="dv-capsule-chart">
        <template v-if="mergedConfig">
            <div class="label-column">
                <div v-for="item in nameList" :key="item.name">
                    {{ item.name }}
                </div>
                <div>&nbsp;</div>
            </div>
            <div class="capsule-container">
                <div class="capsule-item" v-for="(capsule, index) in capsuleLength" :key="index">
                    <div class="capsule-item-column" :style="`width: ${capsule * 100}%; background-color: ${mergedConfig.colors[index % mergedConfig.colors.length]};`">
                        <div v-if="mergedConfig.showValue" class="capsule-item-value">
                            {{ capsuleValue[index] }}
                        </div>
                    </div>
                </div>

                <div class="unit-label">
                    <div v-for="(label, index) in labelData" :key="label + index">
                        {{ label }}
                    </div>
                </div>
            </div>

            <div class="unit-text" v-if="mergedConfig.unit">
                {{ mergedConfig.unit }}
            </div>
        </template>
    </div>
</template>

<style scoped lang="scss">
.dv-capsule-chart {
    position: relative;
    display: flex;
    flex-direction: row;
    box-sizing: border-box;
    padding: 10px;
    color: #fff;

    .label-column {
        display: flex;
        flex-direction: column;
        justify-content: space-between;
        box-sizing: border-box;
        padding-right: 10px;
        text-align: right;
        font-size: 12px;

        div {
            height: 20px;
            line-height: 20px;
        }
    }

    .capsule-container {
        flex: 1;
        display: flex;
        flex-direction: column;
        justify-content: space-between;
    }

    .capsule-item {
        box-shadow: 0 0 3px #999;
        height: 10px;
        margin: 5px 0px;
        border-radius: 5px;

        .capsule-item-column {
            position: relative;
            height: 8px;
            margin-top: 1px;
            border-radius: 5px;
            transition: all 0.3s;
            display: flex;
            justify-content: flex-end;
            align-items: center;

            .capsule-item-value {
                font-size: 12px;
                transform: translateX(100%);
            }
        }
    }

    .unit-label {
        height: 20px;
        font-size: 12px;
        position: relative;
        display: flex;
        justify-content: space-between;
        align-items: center;
    }

    .unit-text {
        text-align: right;
        display: flex;
        align-items: flex-end;
        font-size: 12px;
        line-height: 20px;
        margin-left: 10px;
    }
}
</style>
